package com.qf.controller.manager;

import com.qf.entitys.MerchantClassification;
import com.qf.entitys.RespBean;
import com.qf.service.MerchantClassificationService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/manager/category")
public class CategoryController {

    @Resource
    private MerchantClassificationService merchantClassificationService;
    @GetMapping("/list")
    public String list(HttpServletRequest request){
        List<MerchantClassification> merchantClassifications = merchantClassificationService.selectAll();
        request.setAttribute("mcList",merchantClassifications);
        return "manager/categorylist";
    }

    @GetMapping("/add")
    public String add(){
        return "manager/categoryadd";
    }

    @PostMapping("/add")
    public String add(MerchantClassification mc, MultipartFile file,HttpServletRequest request) throws IOException {

        if(!file.isEmpty()) {
            String realPath = request.getServletContext().getRealPath("/statics/upload/category");
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            String filename = System.currentTimeMillis() + "_" + new Random().nextInt(100000) + "." + extension;
            File f = new File(realPath, filename);
            file.transferTo(f);//上传方法
            mc.setMcIcon(filename);
            int i = merchantClassificationService.insertSelective(mc);

            if (i > 0) {
                return "redirect:list";
            } else {
                return "manager/systemerror";
            }
        }
        return "manager/systemerror";
    }

    @GetMapping("/update")
    public String update(Integer id,HttpServletRequest request){
        MerchantClassification merchantClassification = merchantClassificationService.selectByPrimaryKey(id);
        request.setAttribute("mc",merchantClassification);
        return "manager/categoryupdate";
    }

    @GetMapping("/del")
    @ResponseBody
    public RespBean deleteById(Integer id){
        try {
            int i = merchantClassificationService.deleteByPrimaryKey(id);
            if(i>0){
                return RespBean.success("删除成功");
            }else {
                return RespBean.error("删除失败");
            }
        }catch (Exception ex){
            return RespBean.error("删除失败");
        }
    }

    @GetMapping("/delbyids")
    @ResponseBody
    public RespBean deleteById(@RequestParam("ids[]") Integer[] ids){

        int i = merchantClassificationService.deleteByIds(ids);

        if(i>0){
           return RespBean.success("批量删除成功");
        }else {
           return RespBean.error("批量删除失败");
        }
    }


    @PostMapping("/update")
    @ResponseBody
    public RespBean updateById(MerchantClassification mc, MultipartFile file,HttpServletRequest request) throws IOException {
        if(!file.isEmpty()){
            String realPath = request.getServletContext().getRealPath("/statics/upload/category");
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            String filename = System.currentTimeMillis() + "_" + new Random().nextInt(100000) + "." + extension;
            File f = new File(realPath, filename);
            file.transferTo(f);//上传方法
            mc.setMcIcon(filename);
        }

        int i = merchantClassificationService.updateByPrimaryKeySelective(mc);
        if(i>0){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }

    @GetMapping("/textheader")
    @ResponseBody
    public RespBean testHeader(@RequestHeader String token){
        System.out.println(token);
        return RespBean.success("ok");
    }

    @PostMapping("/textbody")
    @ResponseBody
    public RespBean textbody(@RequestBody MerchantClassification mc){
        System.out.println(mc);
        return RespBean.success("ok");
    }

}
